<template>
  <div class="channel-edit">
    <van-cell  :border="false">
      <div slot="title" class="title-text">我的频道</div>
    <van-button
      class="edit-btn"
      size="mini"
      round
      type="danger"
      plain
      @click="isEdit = !isEdit"
    >{{ isEdit ? '完成': '编辑' }}</van-button>
  </van-cell>
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(channel,index) in myChannels"
        :key="index"
        @click="onMyChannelClick(channel, index)"
      >
        <van-icon slot="icon" name="close" v-show="isEdit && !fiexdChannels.includes(channel.id)"></van-icon>
        <span class="text" :class="{active:index===active}" slot="text">{{channel.name}}</span>
      </van-grid-item>
    </van-grid>
    <van-cell :border="false">
      <div slot="title" class="title-text" >频道推荐</div>
    </van-cell>
    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(channel,index) in recommendChannels"
        :key="index"
        :text="channel.name"
        icon="plus"
        @click="onAddChannel(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels, addUserChannel, deleteUserChannel } from '@/api/channel'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'

export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [],
      isEdit: false,
      fiexdChannels: [0]
    }
  },
  computed: {
    ...mapState(['user']),
    recommendChannels () {
      return this.allChannels.filter((item) => {
        return !this.myChannels.some((myItem) => myItem.id === item.id)
      })
    }
    // recommendChannels () {
    //   const channels = []
    //   this.allChannels.forEach(channel => {
    //     const ret = this.myChannels.find(mychannel => {
    //       return mychannel.id === channel.id
    //     })
    //     if (!ret) {
    //       channels.push(channel)
    //     }
    //   })
    //   return channels
    // }

  },
  watch: {},
  created () {
    this.loadAllChannels()
  },
  mounted () {},
  methods: {
    async loadAllChannels () {
      try {
        const { data } = await getAllChannels()
        this.allChannels = data.data.channels
        console.log(this.allChannels)
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    },
    async onAddChannel (channel) {
      this.myChannels.push(channel)
      if (this.user) {
        try {
          await addUserChannel({
            id: channel.id,
            seq: this.myChannels.length
          })
        } catch (err) {
          this.$toast('保存失败，稍后再试')
        }
      } else {
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },
    onMyChannelClick (channel, index) {
      if (this.isEdit) {
        if (this.fiexdChannels.includes(channel.id)) {
          return
        }
        this.myChannels.splice(index, 1)
        if (index <= this.active) {
          this.$emit('update-active', this.active - 1, true)
        }
        // 执行删除操作
        this.deleteChannel(channel)
      } else {
        // 执行跳转操作
        this.$emit('update-active', index, false)
      }
    },
    async  deleteChannel (channel) {
      try {
        if (this.user) {
          await deleteUserChannel(channel.id)
        } else {
          setItem('TOUTIAO_CHANNELS', this.myChannels)
        }
      } catch (err) {
        this.$toast('操作失败，请稍后再试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  padding: 42px 0;
  .title-text{
    font-size: 16px;
    color: #333;
  }
  .edit-btn{
    width: 52px;
    height: 24px;
    font-size: 13px;
    color: #f85959;
    border: 1px solid #f85959 ;
  }
 /deep/ .grid-item{
    width: 80px;
    height: 43px;
    .van-grid-item__content{
      white-space:nowrap ;
      background-color: #f4f5f6;
      .van-grid-item__text,.text{
        font-size: 14px;
        color: #222;
        margin-top: 0;
      }
      .active{
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }
  /deep/ .van-icon-close {
    position: absolute;
    right: -5px;
    top: -5px;
    font-size: 14px;
    color: #333;
    z-index: 2;
  }
  /deep/.recommend-grid{
    .grid-item{
      .van-grid-item__content{
        flex-direction: row;
        .van-icon {
          font-size: 14px;
          margin-right: 3px;
        }
      }
    }
  }
}
</style>
